package fr.enseirb.mycub.json.impl;

import java.io.IOException;
import java.util.ArrayList;
import org.json.JSONException;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.annotations.SerializedName;

import fr.enseirb.mycub.PrefManager;
import fr.enseirb.mycub.R;
import fr.enseirb.mycub.activity.AbstractPreferencesActivity;
import fr.enseirb.mycub.db.DBAdapter;
import fr.enseirb.mycub.db.InternetDBHelper;
import fr.enseirb.mycub.db.structure.InternetDAO;
import fr.enseirb.mycub.json.JSONHandler;
import fr.enseirb.mycub.task.GetJSONAsyncTask;
import fr.enseirb.mycub.task.GetJSONMockAsyncTask;
import fr.enseirb.mycub.utils.Constants;
import fr.enseirb.mycub.utils.ParserUtils;
import fr.enseirb.mycub.utils.SeekBarPreference;
import android.content.Context;
import android.content.res.Resources.NotFoundException;
import android.util.Log;

public class InternetFullJSONHandler extends JSONHandler{

	public InternetFullJSONHandler(Context context){
		super(context);
		this.preference_key_last_update_date = "InternetJSONHandler_last_update_date";
		this.preference_key_interval_adress = "InternetJSONHandler_interval_adress";
		this.preference_key_interval_between_update = "InternetJSONHandler_interval_between_update";

		this.preference_key_interval_between_update_default = 60000000;
	}

	@Override
	public boolean updateLocalDatabase(String json) {
		Log.i("InternetJSONFull", "update local db");
		boolean success = false;
		DBAdapter dbAdapter = DBAdapter.getInstance(context);
		try {
			//TODO MOCK
			//json = getMock();
			Gson gson = new GsonBuilder().create();
			JsonResponse dao = gson.fromJson(json, JsonResponse.class);
			Log.i("InternetFull","json response :"+json);
			if (dao != null) {
				JsonResponseTranformed dbDAO = new JsonResponseTranformed(dao);
				Log.i("InternetFull","transformation :"+dbDAO.toString());
				InternetDBHelper.cleanAndInsertInternets(dbAdapter.getDB(), dbDAO.itemsDAO);
				success = true;
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return success;
	}

	@Override
	protected void standardUpdate() {
		String baseAddress = Constants.LOCAL_SERVER_URL+"/netaccess/";
		String address = baseAddress+44+"/"+0+"/"+1000000000;
		if(PrefManager.mgr.getBoolean(
				AbstractPreferencesActivity.ENABLE_DISTANCE_FILTER,
				false) && longitude != 1337 && latitude != 1337){
			int radius = PrefManager.mgr.getInt(AbstractPreferencesActivity.DISTANCE_FILTER,
					SeekBarPreference.DEFAULT_DISTANCE);
			address = baseAddress+longitude+"/"+latitude+"/"+radius;
			
		}
		Log.i("JSON Handler", address);
		new GetJSONAsyncTask(this, address).execute();
	}

	public String getMock(){
		try {
			return	ParserUtils.getString(context.getResources().openRawResource(R.raw.internet));
		} catch (NotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

	private class JsonResponse{
		@SerializedName("netAccesses")
		ArrayList<InternetJsonDAO> items;
	}


	private class JsonResponseTranformed{
		ArrayList<InternetDAO> itemsDAO;

		public JsonResponseTranformed(JsonResponse dao){
			itemsDAO = InternetJsonDAO.getDAOList(dao.items);
		}
	}

	static class InternetJsonDAO{
		@SerializedName(ID)
		protected int mId;
		@SerializedName(NAME)
		protected String mName;
		ArrayList<Double> location;
		private String address;
		protected boolean mIsFavorite;
		@SerializedName(FREE)
		private boolean isFree;
		
		public static final String ID = "id";
		public static final String NAME = "name";
		public static final String OPEN = "open";	
		public static final String LATITUDE = "latitude";
		public static final String LONGITUDE = "longitude";
		public static final String FREE = "free";

		public InternetDAO getDAO(){
			InternetDAO item = new InternetDAO();
			item.setId(mId);
			item.setmLatitude(location.get(1));
			item.setmLongitude(location.get(0));
			item.setName(mName);
			item.setOpen(true);
			item.setAddress(getAddress());
			item.setFree(isFree);
			return item;
		}

		public static ArrayList<InternetDAO> getDAOList(ArrayList<InternetJsonDAO> list){
			ArrayList<InternetDAO> finalList = new ArrayList<InternetDAO>();
			int size = list.size();
			for(int i = 0; i < size; i++){
				finalList.add(list.get(i).getDAO());
			}
			return finalList;
		}

		public String getName() {
			return mName;
		}

		public void setName(String name) {
			mName = name;
		}

		public void setId(int id) {
			this.mId = id;
		}

		public int getId() {
			return this.mId;
		}

		public boolean isFavorite() {
			return mIsFavorite;
		}

		public void setFavorite(boolean isFavorite) {
			mIsFavorite = isFavorite;
		}

		public String getAddress() {
			return address;
		}

		public void setAddress(String address) {
			this.address = address;
		}

		public boolean isFree() {
			return isFree;
		}

		public void setFree(boolean isFree) {
			this.isFree = isFree;
		}



	}

}
